package com.z.sys.controller;

import com.github.pagehelper.PageInfo;
import com.z.common.aop.annotation.Log;
import com.z.common.aop.enums.BusinessType;
import com.z.common.web.base.BaseController;
import com.z.common.web.vo.request.PageVo;
import com.z.common.web.vo.response.Result;
import com.z.sys.entity.EmailSend;
import com.z.sys.service.IEmailConfigService;
import com.z.sys.service.IEmailSendService;
import com.z.sys.vo.EmailVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.util.Arrays;

/**
 * 邮件Controller
 *
 * @author z
 * @date 2023-12-28
 */
@Api(tags = "邮件")
@RestController
@RequestMapping("/system/emailSend")
public class EmailSendController extends BaseController {

    private String prefix = "system/emailSend";

    @Autowired
    private IEmailSendService emailSendService;
    @Autowired
    private IEmailConfigService emailConfigService;

    @GetMapping("/main")
    @PreAuthorize("hasPermission('/system/emailSend/main','sys:emailSend:main')")
    @ApiOperation(value = "查询页面")
    public ModelAndView main() {
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询邮件列表
     */
    @ResponseBody
    @GetMapping("/data")
    @PreAuthorize("hasPermission('/system/emailSend/data','sys:emailSend:main')")
    @Log(title = "邮件", describe = "查询邮件列表", type = BusinessType.QUERY)
    @ApiOperation(value = "查询接口")
    public Result list(@ModelAttribute EmailSend emailSend, PageVo pageVo) {
        PageInfo<EmailSend> pageInfo = emailSendService.selectEmailSendPage(emailSend, pageVo);
        return Result.table(pageInfo);
    }

    /**
     * 新增邮件
     */
    @GetMapping("/add")
    @PreAuthorize("hasPermission('/system/emailSend/add','sys:emailSend:add')")
    @ApiOperation(value = "添加页面")
    public ModelAndView add() {
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增邮件
     */
    @ResponseBody
    @PostMapping("/save")
    @PreAuthorize("hasPermission('/system/emailSend/save','sys:emailSend:add')")
    @Log(title = "邮件", describe = "新增邮件数据", type = BusinessType.ADD)
    @ApiOperation(value = "添加接口")
    public Result save(@RequestBody EmailSend emailSend) {
        return Result.decide(emailSendService.save(emailSend));
    }

    /**
     * 修改邮件
     */
    @GetMapping("/edit")
    @PreAuthorize("hasPermission('/system/emailSend/edit','sys:emailSend:edit')")
    @ApiOperation(value = "修改页面")
    public ModelAndView edit(Long id, ModelMap map) {
        EmailSend emailSend =emailSendService.getById(id);
        map.put("emailSend", emailSend);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改邮件
     */
    @ResponseBody
    @PutMapping("/update")
    @PreAuthorize("hasPermission('/system/emailSend/update','sys:emailSend:edit')")
    @Log(title = "邮件", describe = "修改邮件数据", type = BusinessType.EDIT)
    @ApiOperation(value = "修改接口")
    public Result update(@RequestBody EmailSend emailSend) {
        return Result.decide(emailSendService.updateById(emailSend));
    }

    /**
     * 删除邮件
     */
    @ResponseBody
    @DeleteMapping("/batchRemove")
    @PreAuthorize("hasPermission('/system/emailSend/batchRemove','sys:emailSend:del')")
    @Log(title = "邮件", describe = "删除邮件数据", type = BusinessType.REMOVE)
    @ApiOperation(value = "批量删除接口")
    public Result batchRemove(String ids) {
        return Result.decide(emailSendService.removeByIds(Arrays.asList(ids.split(","))));
    }

    /**
     * 删除邮件
     */
    @ResponseBody
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("hasPermission('/system/emailSend/remove','sys:emailSend:del')")
    @Log(title = "邮件", describe = "删除邮件数据", type = BusinessType.REMOVE)
    @ApiOperation(value = "删除接口")
    public Result remove(@PathVariable("id") Long id) {
        return Result.decide(emailSendService.removeById(id));
    }

    /**
     * 发送邮件
     */
    @ResponseBody
    @PostMapping("/send/{id}")
    @PreAuthorize("hasPermission('/system/emailSend/send','sys:emailSend:send')")
    @Log(title = "邮件", describe = "发送邮件数据", type = BusinessType.OTHER)
    @ApiOperation(value = "发送邮件接口")
    public Result send(@PathVariable("id") Long id) {
        EmailSend emailSend = emailSendService.getById(id);
        EmailVo emailVo = new EmailVo(Arrays.asList(new String[]{emailSend.getToUser()}), emailSend.getTitle(), emailSend.getContent());
        File file = new File(emailSend.getFilePath());
        try {
            boolean flag = emailConfigService.send(emailVo, file);
            if(!flag){
                throw new Exception("发送失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("异常：" + e.getMessage());
        }
        return Result.success();
    }

}
